package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletResponse;

import com.limao.DBDataClass.CrowdSmallCompanyClass;
import com.limao.DBDataClass.CrowdUserAuthClass;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Model.RecvData;
import com.limao.Tool.CacheClass;

public class SmallCompanyInfo {

	//&新鲜宝   取办事处详情信息
	public static Object GET_SMALLCOMPANY_OPERATION_DETAIL(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		if (CacheClass.isEmpty(DataObj.smallcompanyid))
			throw new LzPluginException("参数不正确");
		
		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass authTmp=CrowdUserAuthCache.getInstance().getUser(userid);

		int issmallcompanyid=CacheClass.getIntValue(authTmp.issmallcompanyid);
		if(issmallcompanyid==0)
		{
			throw new LzPluginException("无此权限");
		}
		
		CrowdSmallCompanyClass obj=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyByid(DataObj.smallcompanyid.trim(),false);
		if(obj==null)
		{
			throw new LzPluginException("无此办事处或出错");
		}
		
		if(CacheClass.getIntValue(obj.userid)!=userid && CacheClass.getIntValue(obj.pid0)!=issmallcompanyid  && CacheClass.getIntValue(obj.pid0)!=issmallcompanyid)
		{
			throw new LzPluginException("无此权限");
		}
		
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "取办事处详情信息成功";
		goods.obj=obj;
		return goods;
		
	}
	
	
	//&新鲜宝   取下面办事处列表
	public static Object GET_SMALLCOMPANY_OPERATION_LIST(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass authTmp=CrowdUserAuthCache.getInstance().getUser(userid);

		int issmallcompanyid=CacheClass.getIntValue(authTmp.issmallcompanyid);
		if(issmallcompanyid==0)
		{
			throw new LzPluginException("无此权限");
		}
		
		List<CrowdSmallCompanyClass>  list=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyListBysmallcompanyid(issmallcompanyid+"");
		
		List<Object> list2= new ArrayList<Object>();
		if(list!=null && list.size()>0)
			list2.addAll(list);
		
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "办事处列表成功";
		goods.list=list2;
		return goods;
		
	}	

	
	//&新鲜宝   添加办事处
	public static Object ADD_SMALLCOMPANY_OPERATION_INFO(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		if (CacheClass.isEmpty(DataObj.smallcompanyname) || CacheClass.isEmpty(DataObj.mobile)  || CacheClass.isEmpty(DataObj.regular_rate) )
			throw new LzPluginException("参数不正确");
		
		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass authTmp=CrowdUserAuthCache.getInstance().getUser(userid);
		int issmallcompanyid=CacheClass.getIntValue(authTmp.issmallcompanyid);
		if(issmallcompanyid==0)
		{
			throw new LzPluginException("无此权限");
		}
		
		CrowdSmallCompanyClass obj=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyByid(issmallcompanyid+"");
		if(CacheClass.getIntValue(obj.level)>2)
		{
			throw new LzPluginException("无此权限");
		}
		
		CrowdUserAuthClass toAuth=CrowdUserAuthCache.getInstance().getUserBymobile(DataObj.mobile.trim());
		if(toAuth==null)
		{
			throw new LzPluginException("绑定的手机帐号不存在");
		}
		if(CacheClass.getIntValue(toAuth.issmallcompanyid)!=0)
		{
			throw new LzPluginException("绑定的手机帐号已被其它办事处使用");
		}
		if(CacheClass.getIntValue(toAuth.iscompanyid)!=issmallcompanyid)
		{
			throw new LzPluginException("绑定的手机帐号不属于您办事处员工");
		}
		//是否有下线用户
		if(CacheClass.getIntValue(toAuth.total_mannum)>0)
		{
			throw new LzPluginException("绑定的手机帐号已有下级会员");
		}
		
		
		int myRate=CacheClass.getIntValue(obj.regular_rate);
		if(CacheClass.getIntValue(DataObj.regular_rate)==0  || myRate<=CacheClass.getIntValue(DataObj.regular_rate))
		{
			throw new LzPluginException("分成设置不正确");
		}
		
		int marketparentid=CacheClass.getIntValue(obj.marketparentid);
		int pid0=CacheClass.getIntValue(obj.smallcompanyid);
		int pid1=CacheClass.getIntValue(obj.pid0);
		int level=CacheClass.getIntValue(obj.level)+1;
		
		if(!CrowdSmallCompanyCacheManager.getInstance().addSmallCompany(DataObj.smallcompanyname.trim(),toAuth.userid,DataObj.regular_rate.trim(),marketparentid,pid0,pid1,level))
		{
			throw new LzPluginException("添加失败");
		}
		
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "添加办事处成功";
		return goods;
		
	}
	
	//&新鲜宝   添加办事处
	public static Object EDIT_SMALLCOMPANY_OPERATION_INFO(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		if (CacheClass.isEmpty(DataObj.smallcompanyid) || CacheClass.isEmpty(DataObj.smallcompanyname) || CacheClass.isEmpty(DataObj.regular_rate) )
			throw new LzPluginException("参数不正确");
		
		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass authTmp=CrowdUserAuthCache.getInstance().getUser(userid);
		int issmallcompanyid=CacheClass.getIntValue(authTmp.issmallcompanyid);
		if(issmallcompanyid==0)
		{
			throw new LzPluginException("无此权限");
		}
		CrowdSmallCompanyClass tmp=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyByid(issmallcompanyid+"");
		
		
		CrowdSmallCompanyClass obj=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyByid(DataObj.smallcompanyid.trim());
		if(obj==null)
		{
			throw new LzPluginException("办事处不存在");
		}
		
		if(CacheClass.getIntValue(obj.pid0)!=issmallcompanyid)
		{
			throw new LzPluginException("无此权限");
		}
		
		int myRate=CacheClass.getIntValue(tmp.regular_rate);
		if(CacheClass.getIntValue(DataObj.regular_rate)==0  || myRate<=CacheClass.getIntValue(DataObj.regular_rate))
		{
			throw new LzPluginException("分成设置不正确");
		}
		
		if(!CrowdSmallCompanyCacheManager.getInstance().editSmallCompany(DataObj.smallcompanyid.trim(),DataObj.smallcompanyname.trim(),DataObj.regular_rate.trim()))
		{
			throw new LzPluginException("编辑失败");
		}
		
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "编辑办事处成功";
		return goods;
		
	}
		
	
	//&新鲜宝   取办事处结算记录
	public static Object GET_SMALLCOMPANY_OPERATION_DRAWLIST(HttpServletResponse response,AsyncContext asyncContext,int m,RecvData DataObj) throws LzPluginException
	{
		// 参数检查1(默认值赋值)
		int userid=CacheClass.getIntValue(DataObj.userid);
		CrowdUserAuthClass authTmp=CrowdUserAuthCache.getInstance().getUser(userid);

		int issmallcompanyid=CacheClass.getIntValue(authTmp.issmallcompanyid);
		if(issmallcompanyid==0)
		{
			throw new LzPluginException("无此权限");
		}
		
		List<CrowdSmallCompanyClass>  list=new ArrayList<CrowdSmallCompanyClass>();
		//List<CrowdSmallCompanyClass>  list=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyListBysmallcompanyid(issmallcompanyid+"");
		
		List<Object> list2= new ArrayList<Object>();
		if(list!=null && list.size()>0)
			list2.addAll(list);
		
		PublicData goods = new PublicData();
		//生成result包给用户
		goods.state = "1";
		goods.m = m + "";
		goods.info = "取办事处结算记录成功";
		goods.list=list2;
		return goods;
		
	}	
	
	
}
